package com.magicborrow.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.CommonViewPagerActivity;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.utils.ImageUtil;
import com.magicborrow.utils.UIHelper;
import com.magicborrow.utils.VolleyTool;
import com.photoselector.ui.PhotoSelectorActivity;

import java.util.ArrayList;


/**
 * Created by ghost on 15/6/10.
 */
public class AddImgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<String> urls;
    private ArrayList<StuffBean.ImagesEntity> datas;
    private ArrayList<Integer> deleteId;
    private Activity mContext;
    private RecyclerView rv;

    public AddImgAdapter(Activity context, ArrayList<StuffBean.ImagesEntity> datas, RecyclerView rv) {

        this.mContext = context;
        this.rv = rv;
        urls = new ArrayList<>();
        deleteId = new ArrayList<>();
        if (datas == null) {
            this.datas = new ArrayList<>();
        } else {
            this.datas = datas;
            for (StuffBean.ImagesEntity imagesEntity : datas) {
                urls.add(imagesEntity.getOrigin());
            }
        }


    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        // RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(v);
        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        int w = UIHelper.getScreenWidth(mContext) - 2 * mContext.getResources().getDimensionPixelOffset(R.dimen._8dp);
        w = w / 4 - 2 * mContext.getResources().getDimensionPixelOffset(R.dimen._4dp);


        ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(w, w);
        params.setMargins(0, mContext.getResources().getDimensionPixelOffset(R.dimen._6dp), 0, 0);

        imageView.setLayoutParams(params);

        return new RecyclerView.ViewHolder(imageView) {
        };
    }

    public void addData(String url) {
        for (String s : urls) {
            if (s.equals(url)) {
                Toast.makeText(mContext, "您所选的图片与原有图片有重复，\n将自动去除重复", Toast.LENGTH_SHORT).show();
                return;

            }
        }
        urls.add(url);
        notifyItemInserted(urls.size() - 1);
        if (urls.size() >= 8) {
            notifyItemChanged(urls.size());
        }
    }


    public ArrayList<Integer> getDeleteId() {
        return deleteId;
    }

    public ArrayList<String> getAddUrls() {
        ArrayList<String> addUrls = new ArrayList<>();
        for (String s : urls) {
            if (!s.contains("origin")) {
                addUrls.add(s);
            }
        }
        return addUrls;
    }

    public ArrayList<String> getUrls() {
        return urls;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
      /*  if (i >= 9) {
            viewHolder.itemView.setVisibility(View.GONE);
            return;
        } else {
            viewHolder.itemView.setVisibility(View.VISIBLE);

        }*/
        if (urls.size() >= 4) {
            ViewGroup.LayoutParams mParams = rv.getLayoutParams();
            mParams.height = mContext.getResources().getDimensionPixelOffset(R.dimen._110dp) * 2;
            rv.setLayoutParams(mParams);
        } else {
            ViewGroup.LayoutParams mParams = rv.getLayoutParams();
            mParams.height = mContext.getResources().getDimensionPixelOffset(R.dimen._110dp);
            rv.setLayoutParams(mParams);
        }


        viewHolder.itemView.setVisibility(View.VISIBLE);
        if (i >= urls.size()) {
            viewHolder.itemView.setTag("add");
            if (urls.size() >= 8) {
                // viewHolder.itemView.setVisibility(View.GONE);
                // return;
            }
            //viewHolder.imv.setScaleType(ImageView.ScaleType.FIT_XY);
            //  viewHolder.imvDelete.setVisibility(View.GONE);

            ((ImageView) viewHolder.itemView).setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_push_stuff));
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  /*  Intent intent = new Intent(mContext, SelectPicActivity.class);
                    //  (AddContractActivity(mContext)).
                    mContext.startActivityForResult(intent, 0);*/
                    Intent intent = new Intent(mContext, PhotoSelectorActivity.class);
                    intent.putExtra(PhotoSelectorActivity.KEY_MAX, 8 - urls.size());
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    mContext.startActivityForResult(intent, 0);
                }
            });
            return;

        }
        //  viewHolder.imvDelete.setVisibility(View.VISIBLE);
        //viewHolder.imv.setBackgroundResource(0);
        ((ImageView) viewHolder.itemView).setImageDrawable(mContext.getResources().getDrawable(R.mipmap.img_default));
        viewHolder.itemView.setTag(urls.get(i));
        viewHolder.itemView.setId(i);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CommonViewPagerActivity.class);
                intent.putExtra("position", i);
                intent.putExtra("netImageUrls", urls);
                mContext.startActivity(intent);
            }
        });

        if (urls.get(i).contains("store")) {
            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(Constants.ADDRESS + urls.get(i), ((ImageView) viewHolder.itemView));

        } else {

            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage("file://" + urls.get(i), ((ImageView) viewHolder.itemView));

        }
        viewHolder.itemView.setOnLongClickListener(new DeletOnClickListener());


    }

    class DeletOnClickListener implements View.OnLongClickListener {


        @Override
        public boolean onLongClick(View v) {
            String path = (String) v.getTag();
            int id =
                    urls.indexOf(v.getTag());
       /*     for (int i = 0; i < urls.size(); i++) {
                if (urls.get(i).equals(path)) {
                    this.i = i;
                }
            }*/
            if (path.contains("store")) {

                deleteId.add(datas.get(id).getId());
                notifyItemRemoved(id);

                datas.remove(id);
                urls.remove(id);


            } else {
                notifyItemRemoved(id);
                urls.remove(id);

            }
            if (urls.size() < 9) {
                View vAdd = rv.findViewWithTag("add");
                if (vAdd != null) {
                    vAdd.setVisibility(View.VISIBLE);
                } else {
                    notifyItemChanged(urls.size());
                }

            }
            return false;
        }
    }


    @Override
    public int getItemCount() {

        return urls.size() >= 8 ? 8 : urls.size() + 1;
        // return 6;
    }

   /* protected class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imv;
        RelativeLayout rl;

        public ViewHolder(View itemView) {

            super(itemView);
            int w = UIHelper.getScreenWidth(mContext) - 2 * mContext.getResources().getDimensionPixelOffset(R.dimen._8dp);
            w = w / 4 - 2 * mContext.getResources().getDimensionPixelOffset(R.dimen._4dp);
            //  params.setGravity(Gravity.CENTER);
           *//* params2.width = w;
            params2.height = w;*//*
            ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(w, w);

            itemView.setLayoutParams(params2);
            imv = (ImageView) itemView.findViewById(R.id.imv_gallary);



         *//*   imv.setImageDrawable(itemView.getResources().getDrawable(R.mipmap.empty_photo));
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) imv.getLayoutParams();
            Bitmap bitmap = BitmapFactory.decodeResource(itemView.getResources(), R.mipmap.empty_photo);
            rl.setLayoutParams(new LinearLayout.LayoutParams(UIHelper.dip2px(itemView.getContext(), 13) + bitmap.getWidth()
                    , UIHelper.dip2px(itemView.getContext(), 8) + bitmap.getHeight()));


            params.height = bitmap.getHeight();
            params.width = bitmap.getWidth();
            imv.setLayoutParams(params);*//*
        }
    }*/

}
